
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>3.5 多态一对多 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="3.6多态多对多.html" />
    
    
    <link rel="prev" href="3.4多对多.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="1.写在前面的话.html">
            
                <a href="1.写在前面的话.html">
            
                    
                    写在前面的话
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="2.搭建环境.html">
            
                <a href="2.搭建环境.html">
            
                    
                    第一章 搭建环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    第二章 模型定义
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="3.sequelize 实例变量.html">
            
                <a href="3.sequelize 实例变量.html">
            
                    
                    2.1 实例变量方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="4.定义一个模型.html">
            
                <a href="4.定义一个模型.html">
            
                    
                    2.2 定义一个模型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="5.模型字段类型.html">
            
                <a href="5.模型字段类型.html">
            
                    
                    2.3 模型字段类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="6.导入与配置.html">
            
                <a href="6.导入与配置.html">
            
                    
                    2.3 导入与配置
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="3模型关系.html">
            
                <a href="3模型关系.html">
            
                    
                    第三章 模型关系
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="3.1属于.html">
            
                <a href="3.1属于.html">
            
                    
                    3.1 属于
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="3.2包含.html">
            
                <a href="3.2包含.html">
            
                    
                    3.2 包含
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="3.3一对多.html">
            
                <a href="3.3一对多.html">
            
                    
                    3.3 一对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="3.4多对多.html">
            
                <a href="3.4多对多.html">
            
                    
                    3.4 多对多
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.5.5" data-path="3.5多态一对多.html">
            
                <a href="3.5多态一对多.html">
            
                    
                    3.5 多态一对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="3.6多态多对多.html">
            
                <a href="3.6多态多对多.html">
            
                    
                    3.6 多态多对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="3.7关系嵌套.html">
            
                <a href="3.7关系嵌套.html">
            
                    
                    3.7 关系嵌套
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="4模型查询.html">
            
                <a href="4模型查询.html">
            
                    
                    第四章 模型查询
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="5查询范围.html">
            
                <a href="5查询范围.html">
            
                    
                    第五章 查询范围 Scope (预定义查询条件)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="6生命周期函数.html">
            
                <a href="6生命周期函数.html">
            
                    
                    第六章 生命周期函数 Hook
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="7migrate.html">
            
                <a href="7migrate.html">
            
                    
                    第七章 数据库迁移 Migrate
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="8tabel2model.html">
            
                <a href="8tabel2model.html">
            
                    
                    第八章 根据 Table 生成 Model
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >3.5 多态一对多</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x591A;&#x6001;&#x4E00;&#x5BF9;&#x591A;">&#x591A;&#x6001;&#x4E00;&#x5BF9;&#x591A;</h1>
<p>&#x5728;&#x9762;&#x5411;&#x5BF9;&#x8C61;&#x7F16;&#x7A0B;&#x91CC;&#x9762;&#x6709;&#x4E00;&#x4E2A;&#x7279;&#x6027;&#x5C31;&#x662F;&#x591A;&#x6001;&#xFF0C;&#x591A;&#x6001;&#x5C31;&#x662F;&#x9002;&#x5E94;&#x591A;&#x79CD;&#x60C5;&#x51B5;&#xFF0C;&#x6709;&#x7684;&#x65F6;&#x5019;&#x6211;&#x4EEC;&#x6709;&#x8FD9;&#x6837;&#x4E00;&#x4E2A;&#x9700;&#x6C42;&#x3002;&#x8BC4;&#x8BBA;&#x9700;&#x6C42;&#xFF0C;&#x56E0;&#x4E3A;&#x7F51;&#x7AD9;&#x672C;&#x6765;&#x5C31;&#x4E0D;&#x5927;&#xFF0C;&#x6240;&#x4EE5;&#x6211;<strong>&#x671F;&#x671B;&#x6240;&#x6709;&#x7684;&#x8BC4;&#x8BBA;&#x90FD;&#x653E;&#x5728;&#x4E00;&#x4E2A;&#x8868;&#x91CC;&#x9762;&#xFF0C;&#x53EF;&#x4EE5;&#x8BC4;&#x8BBA;&#x56FE;&#x7247;&#xFF0C;&#x8BC4;&#x8BBA;&#x6587;&#x7AE0;</strong>&#x7B49;&#x7B49;&#x3002;</p>
<p>&#x9996;&#x5148;&#x8BC4;&#x8BBA;&#x8FD9;&#x4E2A;&#x662F;&#x4E00;&#x5BF9;&#x591A;&#x7684;&#x5173;&#x7CFB;&#x3002;&#x6240;&#x4EE5;&#x8BF4;&#x5E94;&#x8BE5;&#x5728;&#x8BC4;&#x8BBA;&#x8868;&#x91CC;&#x9762;&#x5B58;&#x50A8;&#x4E00;&#x4E2A;<strong>&#x88AB;&#x8BC4;&#x8BBA;&#x6A21;&#x578B;&#xFF08;&#x6BD4;&#x5982;&#x6587;&#x7AE0;&#x3001;&#x56FE;&#x7247;&#xFF09;</strong>&#x7684; Id&#xFF0C;&#x4F46;&#x662F;&#x8FD9;&#x4E2A;&#x65F6;&#x5019;&#x5462;&#xFF0C;&#x6587;&#x7AE0;&#x7684; ID &#x4E0E;&#x56FE;&#x7247;&#x7684; ID &#x4E00;&#x5B9A;&#x4F1A;&#x6709;&#x91CD;&#x5408;&#x7684;&#x60C5;&#x51B5;&#x3002;&#x4F8B;&#x5982;&#x67D0;&#x7528;&#x6237;&#x8BC4;&#x8BBA;&#x4E86; Id &#x4E3A; 1&#x7684;&#x6587;&#x7AE0;&#xFF0C;&#x67D0;&#x7528;&#x6237;&#x8BC4;&#x8BBA;&#x4E86; Id &#x4E3A;1&#x7684;&#x56FE;&#x7247;&#xFF0C;&#x8FD9;&#x4E2A;&#x65F6;&#x5019;&#x6211;&#x4EEC;&#x67E5;&#x8BE2;&#x7684;&#x65F6;&#x5019;&#x5C31;&#x4E0D;&#x6E05;&#x695A;&#x8FD9;&#x4E2A;&#x7528;&#x6237;&#x5230;&#x5E95;&#x662F;&#x8BC4;&#x8BBA;&#x4E86;&#x56FE;&#x7247;&#x8FD8;&#x8BF4;&#x6587;&#x7AE0;&#x3002;</p>
<p>&#x9488;&#x5BF9;&#x8FD9;&#x4E00;&#x95EE;&#x9898;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5728;&#x591A;&#x7684;&#x8FD9;&#x4E00;&#x65B9;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BC4;&#x8BBA;&#x8868;&#x91CC;&#x9762;&#x589E;&#x52A0;&#x4E00;&#x4E2A;&#x5B57;&#x6BB5;&#xFF0C;&#x6BD4;&#x5982; type&#xFF0C;&#x5F53; type &#x4E3A; post &#x7684;&#x65F6;&#x5019;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x77E5;&#x9053;&#x662F;&#x6587;&#x7AE0;&#xFF0C;&#x5F53; type &#x4E3A; image &#x7684;&#x65F6;&#x5019;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x77E5;&#x9053;&#x662F;&#x56FE;&#x7247;&#x3002;</p>
<blockquote>
<p>&#x5F53;&#x7136;&#x8FD9;&#x4E2A;&#x4E1C;&#x897F;&#x53EF;&#x80FD;&#x6709;&#x66F4;&#x4E13;&#x4E1A;&#x7684;&#x540D;&#x79F0; (association scopes) &#xFF0C;&#x4F46;&#x662F;&#x6211;&#x66F4;&#x559C;&#x6B22;&#x628A;&#x5B83;&#x7406;&#x89E3;&#x4E3A;&#x591A;&#x6001;&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x66F4;&#x5FEB;&#x901F;&#x7684;&#x5B9E;&#x73B0;&#xFF0C;&#x5C31;&#x4E0D;&#x518D;&#x5199;&#x63A5;&#x53E3;&#x4E86;&#x3002;&#x5173;&#x4E8E;&#x5982;&#x4F55;&#x5B9A;&#x4E49;&#x63A5;&#x53E3;&#x8BA9;&#x4EE3;&#x7801;&#x63D0;&#x793A;&#x6B63;&#x5E38;&#x5DE5;&#x4F5C;&#xFF0C;&#x524D;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x5DF2;&#x7ECF;&#x57FA;&#x672C;&#x6F14;&#x793A;&#x5B8C;&#x4E86;&#x3002;</p>
</blockquote>
<h3 id="&#x4F8B;&#x5B50;">&#x4F8B;&#x5B50;</h3>
<h4 id="&#x65B0;&#x5EFA;-commentts">&#x65B0;&#x5EFA; comment.ts</h4>
<pre><code class="lang-ts"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> (sequelize, dataTypes) =&gt; {
  <span class="hljs-keyword">const</span> Comment = sequelize.define(<span class="hljs-string">&apos;Comment&apos;</span>, {
    text: dataTypes.STRING,
    <span class="hljs-keyword">type</span>: dataTypes.STRING,
    type_id: dataTypes.INTEGER
  });

  Comment.associate = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">models</span>) </span>{
    <span class="hljs-keyword">this</span>.belongsTo(models.Post);
    <span class="hljs-keyword">this</span>.belongsTo(models.Image);
  }

  <span class="hljs-keyword">return</span> Comment;

}
</code></pre>
<h4 id="&#x65B0;&#x5EFA;-postts">&#x65B0;&#x5EFA; post.ts</h4>
<pre><code class="lang-ts"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> (sequelize, dataTypes) =&gt; {
  <span class="hljs-keyword">const</span> Post = sequelize.define(<span class="hljs-string">&apos;Post&apos;</span>, {
    title: dataTypes.STRING,
    content: dataTypes.TEXT,
  });

  Post.associate = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">models</span>) </span>{
    <span class="hljs-keyword">this</span>.hasMany(models.Comment, {
      foreignKey: <span class="hljs-string">&apos;type_id&apos;</span>,
      constraints: <span class="hljs-literal">false</span>,
      scope: {
        <span class="hljs-keyword">type</span>: <span class="hljs-string">&apos;post&apos;</span>
      }
    });
  }

 <span class="hljs-keyword">return</span> Post;
}
</code></pre>
<p>&#x591A;&#x6001;&#x90FD;&#x662F;&#x901A;&#x8FC7;&#x589E;&#x52A0;&#x4E00;&#x4E2A;&#x6807;&#x8BC6;&#x5B57;&#x6BB5;&#x6765;&#x533A;&#x5206;&#x4E0D;&#x540C;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x800C;&#x5728;&#x58F0;&#x660E;&#x5173;&#x7CFB;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; scope &#x6765;&#x6307;&#x5B9A;&#x8FD9;&#x4E2A;&#x6807;&#x8BC6;&#x662F;&#x4EC0;&#x4E48;&#xFF0C;&#x8FD9;&#x6837;&#x5C31;&#x53EF;&#x4EE5;&#x533A;&#x5206;&#x5F00;&#x6765;&#x4E86;&#x3002;constraints &#x662F;&#x4E3A;&#x4E86;&#x5173;&#x95ED;&#x5916;&#x952E;&#x6240;&#x5B58;&#x5728;&#x7684;&#x7EA6;&#x675F;&#xFF0C;&#x5BF9;&#x4E8E;&#x591A;&#x6001;&#x7684;&#xFF0C;&#x8BF7;&#x5173;&#x95ED;&#x5B83;&#x3002;foreignKey &#x662F;&#x4E3A;&#x4E86;&#x6307;&#x5B9A;&#x76F8;&#x5173;&#x8054;&#x7684;&#x5B57;&#x6BB5;&#x503C;&#x3002;</p>
<h4 id="&#x65B0;&#x5EFA;-imagets">&#x65B0;&#x5EFA; image.ts</h4>
<pre><code class="lang-ts"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> (sequelize, dataTypes) =&gt; {
  <span class="hljs-keyword">const</span> Image = sequelize.define(<span class="hljs-string">&apos;Image&apos;</span>, {
    title: dataTypes.STRING,
    imageURL: dataTypes.STRING,
  });

  Image.associate = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">models</span>) </span>{
    <span class="hljs-keyword">this</span>.hasMany(models.Comment, {
      foreignKey: <span class="hljs-string">&apos;type_id&apos;</span>,
      constraints: <span class="hljs-literal">false</span>,
      scope: {
        <span class="hljs-keyword">type</span>: <span class="hljs-string">&apos;image&apos;</span>
      }
    });
  }

  <span class="hljs-keyword">return</span> Image;
}
</code></pre>
<h4 id="&#x5728;-indexts-&#x91CC;&#x9762;&#x6DFB;&#x52A0;&#x5982;&#x4E0B;&#x4EE3;&#x7801;">&#x5728; index.ts &#x91CC;&#x9762;&#x6DFB;&#x52A0;&#x5982;&#x4E0B;&#x4EE3;&#x7801;</h4>
<pre><code class="lang-ts"><span class="hljs-keyword">const</span> Comment = sequelize.import(<span class="hljs-string">&apos;./comment&apos;</span>);
<span class="hljs-keyword">const</span> Post = sequelize.import(<span class="hljs-string">&apos;./post&apos;</span>);
<span class="hljs-keyword">const</span> Image = sequelize.import(<span class="hljs-string">&apos;./image&apos;</span>);
   <span class="hljs-keyword">let</span> post = await Post.create({
 title: <span class="hljs-string">&apos;name&apos;</span>,
 content: <span class="hljs-string">&apos;some&apos;</span>
});

<span class="hljs-keyword">let</span> image = await Image.create({
 title: <span class="hljs-string">&apos;image&apos;</span>,
 imageURL: <span class="hljs-string">&apos;url&apos;</span>
});

<span class="hljs-built_in">console</span>.log((post as <span class="hljs-built_in">any</span>).__proto__)

await (post as <span class="hljs-built_in">any</span>).createComment({
 text: <span class="hljs-string">&apos;hello&apos;</span>
});

await (image as <span class="hljs-built_in">any</span>).createComment({
 text: <span class="hljs-string">&apos;hello2&apos;</span>
});
</code></pre>
<p>&#x6253;&#x5370;&#x4E00;&#x4E0B; (post as any).<strong>proto</strong> &#x6211;&#x4EEC;&#x4FBF;&#x53EF;&#x77E5;&#x9053;&#x5B83;&#x6709;&#x54EA;&#x4E9B;&#x5173;&#x4E8E; comment &#x7684;&#x65B9;&#x6CD5;&#x3002;</p>
<pre><code>getComments: [Function]
countComments: [Function]
hasComment: [Function]
hasComments: [Function]
setComments: [Function]
addComment: [Function]
addComments: [Function]
removeComment: [Function]
removeComments: [Function]
createComment: [Function]
</code></pre><p>&#x8BB0;&#x5F97;&#x522B;&#x5FD8;&#x4E86;&#x540C;&#x6B65;&#x6570;&#x636E;&#x5E93;&#x8868;&#xFF0C;&#x5B8C;&#x6210;&#x8FD0;&#x884C;&#x4E4B;&#x540E;&#xFF0C;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x6240;&#x6709;&#x7684;&#x8BC4;&#x8BBA;&#x90FD;&#x5B58;&#x5728;&#x4E86;&#x4E00;&#x4E2A;&#x8868;&#x91CC;&#x9762;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="3.4多对多.html" class="navigation navigation-prev " aria-label="Previous page: 3.4 多对多">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="3.6多态多对多.html" class="navigation navigation-next " aria-label="Next page: 3.6 多态多对多">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"3.5 多态一对多","level":"1.5.5","depth":2,"next":{"title":"3.6 多态多对多","level":"1.5.6","depth":2,"path":"document/3.6多态多对多.md","ref":"./document/3.6多态多对多.md","articles":[]},"previous":{"title":"3.4 多对多","level":"1.5.4","depth":2,"path":"document/3.4多对多.md","ref":"./document/3.4多对多.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"document/3.5多态一对多.md","mtime":"2017-06-27T03:45:21.000Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-06-28T08:29:37.466Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

